﻿using System;
using System.Xml.Linq;
using System.Collections.ObjectModel;
using System.Text;

namespace WarioOldies.Models
{
    public class GameList
    {
        /// <summary>
        /// Sauvegarde
        /// </summary>
        /// <param name="?"></param>

        public void Save( string filename, ObservableCollection<Game> games )
        {
            XDocument document = new XDocument(); 
            
            XElement gamesElement = new XElement("Games");


            document.Add(gamesElement);

            foreach (Game game in games)
            {
                // Obligatoire
                XElement gameElement = new XElement("Game");

                if (string.IsNullOrWhiteSpace(game.Machine) == false)
                    gameElement.Add(new XAttribute("Machine", game.Machine));

                if (string.IsNullOrWhiteSpace(game.Label) == false)
                    gameElement.Add(new XAttribute("Label", game.Label.Trim()));

                if (string.IsNullOrWhiteSpace(game.Id) == false)
                    gameElement.Add(new XAttribute("Id", game.Id.Trim()));

                if (string.IsNullOrWhiteSpace(game.RomUri) == false)
                    gameElement.Add(new XElement("RomRelativeUri", game.RomUri.Trim()));
                
                if (string.IsNullOrWhiteSpace(game.Genre) == false)
                    gameElement.Add(new XAttribute("Genre", game.Genre.Trim()));

                if (string.IsNullOrWhiteSpace(game.Publisher) == false)
                    gameElement.Add(new XAttribute("Publisher", game.Publisher.Trim()));

                if( string.IsNullOrWhiteSpace( game.Language ) == false  )
                    gameElement.Add(new XElement("Language", game.Language.Trim()));

                if (string.IsNullOrWhiteSpace(game.Serie) == false)
                    gameElement.Add(new XElement("Serie", game.Serie.Trim()));

                if (string.IsNullOrWhiteSpace(game.FrontCoverUri) == false)
                    gameElement.Add(new XElement("FrontCoverRelativeUri", game.FrontCoverUri.Trim()));

                if (string.IsNullOrWhiteSpace(game.Description) == false)
                    gameElement.Add(new XElement("Description", game.Description.Trim()));

                gamesElement.Add(gameElement);
            }

            document.Save(filename);
        }

        /// <summary>
        /// Chargement de la liste de jeux
        /// </summary>
        /// <param name="document"></param>
        /// <returns></returns>

        public ObservableCollection<Game> Load(string filename)
        {
            XDocument document = XDocument.Load(filename);

            ObservableCollection < Game > result = new ObservableCollection<Game>();

            XElement games = document.Root;

            foreach (XElement game in games.Elements("Game"))
            {
                Game g = new Game();

                XAttribute machine = game.Attribute("Machine");
                if (machine != null && string.IsNullOrWhiteSpace(machine.Value) == false)
                    g.Machine = machine.Value.Trim();

                XAttribute label = game.Attribute("Label");
                if (label != null && string.IsNullOrWhiteSpace(label.Value) == false)
                    g.Label = label.Value.Trim();

                XAttribute id = game.Attribute("Id");
                if (id != null && string.IsNullOrWhiteSpace(id.Value) == false)
                    g.Id = id.Value;

                XAttribute genre = game.Attribute("Genre");
                if (genre != null && string.IsNullOrWhiteSpace(genre.Value) == false)
                    g.Genre = genre.Value;

                XAttribute publisher = game.Attribute("Publisher");
                if (publisher != null && string.IsNullOrWhiteSpace(publisher.Value) == false)
                    g.Publisher = publisher.Value;

                /* optional */

                XElement language = game.Element("Language");
                if (language != null)
                    g.Language = language.Value;

                XElement serie = game.Element("Serie");
                if (serie != null)
                    g.Serie = serie.Value;

                XElement frontCoverPartUri = game.Element("FrontCoverRelativeUri");
                if (frontCoverPartUri != null)
                    g.FrontCoverUri = frontCoverPartUri.Value;
                //g.FrontCoverUri = server + frontCoverPartUri.Value;

                XElement description = game.Element("Description");

                if (description != null)
                {
                    // Description
                    string[] lines = description.Value.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

                    StringBuilder descriptionBuilder = new StringBuilder();

                    foreach (string line in lines)
                    {
                        descriptionBuilder.AppendLine(line.Trim());
                    }

                    g.Description = descriptionBuilder.ToString();

                    // petite description
                    string smallDescription = g.Description.Replace("\n", " ");
                    smallDescription = smallDescription.Replace("\r", "");

                    if (smallDescription.Length > 100)
                    {
                        smallDescription = smallDescription.Substring(0, 100);
                    }


                    g.SmallDescription = smallDescription;
                }

                // La rom est obligatoire

                //g.RomUri = server + game.Element("RomRelativeUri").Value;

                XElement romElement = game.Element("RomRelativeUri");

                if( romElement != null )
                    g.RomUri = romElement.Value;

                result.Add(g);
            }
            

            return result;
        }
    }
}
